Managing Dependencies Using pip and requirements.txt
Course: Python Training
Topic: Managing Dependencies Using pip and requirements.txt
Course URL: Python Training Course
Demo URL: Register for a Course Demo
JustAcademy Website: Visit JustAcademy
1. Introduction to Dependency Management
Dependency management is the process of identifying, installing, organizing, updating, and maintaining the external packages required by a software application. Python developers frequently use third-party libraries to implement features without writing every function from scratch.
For example, a Python web application might use Flask for routing, requests for HTTP communication, and pytest for automated testing. These packages are dependencies because the application or its development workflow relies on them.
Python provides pip to install and manage packages. A requirements.txt file is commonly used to document the packages and version constraints needed to set up a project.
Why Dependency Management Matters
- Ensures that applications have access to the required packages.
- Helps developers recreate project environments on different computers.
- Reduces compatibility problems between package versions.
- Simplifies application deployment and team collaboration.
- Makes project setup easier for new developers.
- Supports testing, maintenance, and controlled package upgrades.
2. Learning Objectives
After studying this topic, learners will be able to:
- Understand Python dependencies and package management.
- Explain the purpose and functionality of pip.
- Install, upgrade, inspect, and uninstall Python packages.
- Create and activate virtual environments.
- Create and maintain requirements.txt files.
- Install dependencies from a requirements file.
- Understand package version constraints.
- Identify direct and transitive dependencies.
- Troubleshoot common package installation errors.
- Apply dependency management practices in real-world Python projects.
3. Understanding Python Dependencies
A dependency is a library, package, or other software component that a program needs to operate correctly or perform a particular task.
Python dependencies can be installed from package indexes such as the Python Package Index (PyPI), from compatible source distributions, or from other supported package locations.
Example of a Dependency
Suppose an application needs to send HTTP requests to a website. Instead of implementing HTTP communication from scratch, the developer can use the requests package.
import requests
response = requests.get(
"https://example.com",
timeout=10
)
print("Status code:", response.status_code)
The requests package is an external dependency. It must be installed in the Python environment used to run this program.
Types of Dependencies
| Dependency Type |
Description |
Example |
| Direct dependency |
A package explicitly required by the project. |
requests |
| Transitive dependency |
A package required by another dependency. |
A supporting library required by requests. |
| Runtime dependency |
A package needed while the application runs. |
Flask |
| Development dependency |
A package used for development, testing, or code formatting. |
pytest, black |
4. What Is pip?
pip is the standard package installer commonly used in Python development. It installs packages, resolves their declared dependencies, upgrades existing installations, removes packages, and displays package information.
Developers often use pip with a virtual environment so that packages are installed separately for each project.
Checking the pip Version
Run the following command in your terminal:
python -m pip --version
On systems where Python is accessed through python3, use:
python3 -m pip --version
Using python -m pip helps ensure that pip runs through the selected Python interpreter.
Common pip Commands
| Command |
Purpose |
python -m pip --version |
Displays the pip version and installation location. |
python -m pip install requests |
Installs the requests package. |
python -m pip install --upgrade requests |
Upgrades requests within applicable constraints. |
python -m pip uninstall requests |
Uninstalls the requests package. |
python -m pip show requests |
Displays information about requests. |
python -m pip list |
Lists installed packages. |
python -m pip freeze |
Displays installed distributions and versions in requirements-style format. |
python -m pip check |
Checks installed packages for incompatible dependency requirements. |
5. Installing Packages Using pip
Installing packages is one of the most common dependency management tasks in Python. A package can be installed by providing its name after the install command.
Step 1: Verify Python Installation
python --version
If this command is unavailable, check whether Python is installed and accessible through your system's PATH.
Step 2: Install a Package
python -m pip install requests
pip downloads a compatible distribution and installs it into the active environment, along with required dependencies when necessary.
Step 3: Verify the Installation
python -m pip show requests
You can also check whether the package imports correctly:
python -c "import requests; print(requests.__version__)"
Installing Multiple Packages
Multiple packages can be installed in a single command by separating their names with spaces.
python -m pip install requests flask pytest
For larger projects, keeping package requirements in a file makes the setup process easier to repeat and maintain.
6. What Is requirements.txt?
requirements.txt is a plain-text file commonly used to specify the Python packages that a project needs. Each entry can identify a package and, where appropriate, specify a version or version constraint.
The file allows developers to install multiple dependencies together rather than manually installing each package.
Example requirements.txt
requests==2.32.3
Flask>=3.0,<4.0
pytest>=8.0
These entries demonstrate common version-specification formats. Before using specific versions in a real project, check their compatibility with the project's Python version and other dependencies.
Explanation of the Entries
| Entry |
Explanation |
requests==2.32.3 |
Requires requests version 2.32.3. |
Flask>=3.0,<4.0 |
Allows Flask versions from 3.0 up to, but not including, 4.0. |
pytest>=8.0 |
Allows pytest version 8.0 or newer, subject to other constraints. |
7. Understanding Package Version Constraints
Python packages use version numbers to identify releases. Version constraints help developers control which releases pip can select.
| Operator |
Meaning |
Example |
== |
Matches an exact version. |
requests==2.32.3 |
>= |
Sets a minimum version. |
pytest>=8.0 |
< |
Excludes the specified version and newer releases. |
Flask<4.0 |
!= |
Excludes a particular version. |
package-name!=1.2.0 |
~= |
Specifies a compatible release range. |
requests~=2.32.0 |
Why Version Constraints Are Important
- They help prevent unexpected upgrades from introducing breaking changes.
- They document which releases the application is intended to support.
- They help pip resolve compatible versions across dependencies.
- They improve repeatability when used as part of a controlled installation process.
Exact version pins can make installations more predictable, while bounded ranges allow compatible updates. For highly reproducible deployments, a tested dependency lock and controlled build environment may also be necessary.
8. Creating and Using Virtual Environments
A virtual environment is an isolated Python environment containing its own package installation area. It allows different projects to use different dependency versions without unnecessarily affecting one another.
For example, one application may require a particular Django release while another application needs a different release. Separate virtual environments help manage these requirements safely.
Step 1: Create a Project Directory
mkdir dependency_demo
cd dependency_demo
Step 2: Create a Virtual Environment
python -m venv .venv
This command creates a virtual environment in a directory named .venv.
Step 3: Activate the Virtual Environment
Windows Command Prompt:
.venv\Scripts\activate.bat
Windows PowerShell:
.\.venv\Scripts\Activate.ps1
macOS or Linux:
source .venv/bin/activate
After activation, the environment name often appears in the terminal prompt.
Step 4: Install a Dependency
python -m pip install requests
The package is installed into the active virtual environment, helping keep it separate from other projects.
Step 5: Deactivate the Environment
deactivate
This command exits the active virtual environment. The environment itself remains available for later use.
9. Creating a requirements.txt File
A requirements file can be written manually or generated from an existing Python environment. The appropriate method depends on whether you want to document intended project dependencies or capture the installed environment.
Method 1: Create the File Manually
Create a file named requirements.txt in the project directory.
requests==2.32.3
Flask>=3.0,<4.0
Manual editing gives developers control over which packages are declared and which version constraints are applied.
Method 2: Generate the File Using pip freeze
First, activate the project's virtual environment and install the packages required by the project. Then run:
python -m pip freeze > requirements.txt
The command redirects the output of pip freeze into the requirements file. If the file already exists, its previous contents are replaced.
Example Output
Flask==3.1.0
requests==2.32.3
urllib3==2.2.3
The versions above are illustrative. The actual output depends on the packages installed in your environment.
Manual File vs. pip freeze
| Manual requirements.txt |
pip freeze Output |
| Can focus on direct project dependencies. |
Generally records all installed distributions visible in the environment. |
| Allows deliberate version ranges. |
Typically records exact installed versions. |
| Requires manual maintenance as requirements change. |
Can capture unrelated packages if the environment is not clean. |
| Useful for documenting intended dependencies. |
Useful for recording an installed environment snapshot. |
Best practice: Generate pip freeze output from a dedicated virtual environment. Review the file before committing it to version control, especially if the environment contains tools that are not part of the application.
10. Installing Dependencies from requirements.txt
Once a requirements file has been created, all the listed requirements can be installed with a single command.
python -m pip install -r requirements.txt
The -r option tells pip to read installation requirements from the specified file.
Recommended Setup Workflow
- Download or clone the project repository.
- Open a terminal in the project directory.
- Create a virtual environment.
- Activate the virtual environment.
- Install dependencies from requirements.txt.
- Check dependency compatibility.
- Run the application or automated tests.
python -m venv .venv
# Activate the environment using the command for your OS.
python -m pip install -r requirements.txt
python -m pip check
This workflow provides a consistent setup procedure for developers and helps detect dependency issues before the application is run.
11. Updating Python Dependencies
Dependencies may need updates to obtain security patches, bug fixes, new features, or compatibility improvements. However, package updates can sometimes introduce breaking changes.
Check Package Information
python -m pip show requests
Upgrade a Package
python -m pip install --upgrade requests
pip selects an available version that satisfies the applicable requirements and constraints.
Update the Requirements File
If the project specifies an exact version, review and update the version constraint when a different release is intended. Then install the updated requirements:
python -m pip install -r requirements.txt
This command installs packages according to the file; it does not automatically rewrite the file or guarantee that all dependencies are upgraded to the newest available versions.
After updating dependencies, run automated tests and check the application for regressions.
12. Uninstalling Dependencies
Unused packages can increase maintenance overhead and complicate the dependency environment. Removing them may simplify the project, but first confirm that the package is not required by the application or another dependency.
python -m pip uninstall requests
Removing a package from the environment does not automatically remove its entry from requirements.txt. Edit the file separately when the project no longer needs that dependency.
After making changes, run:
python -m pip check
13. Direct and Transitive Dependencies
A direct dependency is explicitly declared by the project. A transitive dependency is required by another package in the dependency graph.
For example, an application might directly depend on requests, while requests depends on additional libraries for supporting functionality. Those supporting libraries are transitive dependencies of the application.
Important Considerations
- Changing a direct dependency can affect the versions of transitive dependencies.
- Incompatible package requirements can prevent a successful installation.
- Package availability may vary across operating systems and Python versions.
- A list of direct dependencies alone may not fully describe an exact environment.
- Production projects may benefit from a dedicated dependency-locking workflow.
14. Managing Development and Production Dependencies
Development tools and production packages often serve different purposes. Separating them can make the setup process clearer and help avoid installing unnecessary development tools in production.
Example Project Structure
my_project/
├── app.py
├── requirements.txt
├── requirements-dev.txt
├── tests/
└── README.md
requirements.txt
Flask>=3.0,<4.0
requests>=2.32,<3.0
This file contains example runtime dependencies.
requirements-dev.txt
-r requirements.txt
pytest>=8.0
black>=24.0
This file includes the runtime requirements and adds example testing and formatting tools.
Install Development Dependencies
python -m pip install -r requirements-dev.txt
This approach is suitable for straightforward projects. Larger projects may use pyproject.toml with a dependency manager that supports development groups and lock files.
15. Common requirements.txt Syntax
A requirements file can contain package names, version constraints, comments, and references to other requirement files. pip also supports certain URL-based, local-path, and editable-install requirements.
| Syntax |
Purpose |
requests |
Declares a package without a version constraint. |
requests==2.32.3 |
Specifies an exact package version. |
Flask>=3.0,<4.0 |
Specifies a permitted version range. |
-r requirements-dev.txt |
Includes requirements from another file. |
# Development tools |
Adds a comment to explain a section. |
Use advanced requirement formats carefully because local paths, external repositories, and direct URLs can make project installation dependent on additional resources or specific environment configurations.
16. Common Dependency Management Errors
Error 1: pip Is Not Recognized
Cause: Python or pip is not available through the terminal's PATH, or the intended environment is not activated.
Solution: Try running pip through Python:
python -m pip --version
If Python itself is unavailable, verify the installation and use the appropriate Python executable.
Error 2: ModuleNotFoundError
Cause: The required package is missing from the Python environment used to run the program.
Solution: Activate the correct virtual environment and install the package:
python -m pip install requests
Also confirm that your code editor uses the same interpreter.
Error 3: Dependency Conflicts
Cause: Two or more packages require incompatible versions of a shared dependency.
Solution: Review the installation error, inspect version constraints, and select compatible versions. Then run tests to verify the result.
python -m pip check
Error 4: Permission Denied
Cause: The current user cannot write to the target Python installation directory.
Solution: Use a virtual environment so that packages can be installed in a project-specific location without modifying the system-wide installation.
Error 5: Package Installation Fails
Cause: The package may not support the installed Python version, a network connection may be unavailable, or a compatible distribution or required build tool may be missing.
Solution: Review the complete error message, check the package's supported Python versions, verify network access, and follow the package's installation instructions.
Error 6: requirements.txt Is Out of Date
Cause: Packages were installed, upgraded, or removed without updating the project's dependency declarations.
Solution: Review the project's intended dependencies and update the requirements file manually or regenerate it from the correct virtual environment.
17. Dependency Management Best Practices
- Use virtual environments: Isolate each project's dependencies.
- Maintain dependency documentation: Keep requirements.txt or the chosen dependency configuration up to date.
- Choose version constraints carefully: Balance flexibility and repeatability.
- Use a clean environment: Avoid recording unrelated globally installed packages.
- Test package updates: Run automated tests after dependency changes.
- Review security advisories: Check dependencies for known vulnerabilities and update when appropriate.
- Remove unnecessary packages: Reduce complexity and maintenance overhead.
- Exclude virtual environments from Git: Avoid committing the .venv directory.
- Document setup instructions: Explain how to create an environment and install project dependencies.
- Review third-party packages: Consider package maintenance, licensing, security, and trustworthiness.
- Use controlled deployment processes: Validate dependency versions before releasing an application.
18. requirements.txt vs. pyproject.toml
Both files are used in Python projects, but their purposes differ.
| Feature |
requirements.txt |
pyproject.toml |
| Primary purpose |
Lists package requirements for installation workflows. |
Defines project metadata and configuration for Python packaging and development tools. |
| Dependency declaration |
Commonly used to list packages and version constraints. |
Can declare project dependencies through standardized metadata. |
| Installation |
python -m pip install -r requirements.txt |
A configured project can be installed using pip, including its declared dependencies. |
| Dependency locking |
Can pin versions but does not inherently provide a complete lock workflow. |
Does not inherently lock dependencies; additional tooling may be needed. |
| Common use |
Application setup and deployment. |
Python package metadata and development-tool configuration. |
A project can use both files when needed. Maintain a clear source of truth for dependency declarations and avoid conflicting dependency lists.
19. Practical Project: Build a Python Application
In this practical exercise, you will create a Python project, install a dependency, generate a requirements file, and recreate the environment from that file.
Step 1: Create the Project
mkdir api_demo
cd api_demo
python -m venv .venv
Activate the virtual environment using the appropriate command for your operating system.
Step 2: Install the Dependency
python -m pip install requests
Step 3: Generate requirements.txt
python -m pip freeze > requirements.txt
Open the generated file and check that requests and its installed dependencies are listed.
Step 4: Create app.py
import requests
def check_website(url):
try:
response = requests.get(url, timeout=10)
print("URL:", url)
print("Status:", response.status_code)
except requests.RequestException as error:
print("Request failed:", error)
if __name__ == "__main__":
check_website("https://example.com")
The application sends an HTTP request and displays the response status. An active network connection is required for the request to succeed.
Step 5: Run the Application
python app.py
Step 6: Recreate the Environment
To test reproducibility, create a fresh virtual environment, activate it, and install the recorded requirements.
python -m pip install -r requirements.txt
python -m pip check
python app.py
This exercise demonstrates how a dependency file can simplify project setup on another machine or in a clean environment.
20. Troubleshooting Checklist
- Confirm that Python is installed and accessible.
- Verify that the correct virtual environment is activated.
- Check which Python interpreter your editor is using.
- Use
python -m pip with the intended interpreter.
- Confirm that requirements.txt exists in the expected directory.
- Check package names and version constraints for mistakes.
- Verify that the Python version is supported by the dependencies.
- Run
python -m pip check to identify installed dependency conflicts.
- Review network, permission, and package-build errors.
- Run automated tests after installing or updating dependencies.
21. Practice Exercises
Beginner Exercises
- Display the installed Python and pip versions.
- Install requests and verify its version.
- List all packages installed in the active environment.
- Create and activate a virtual environment.
- Create a requirements.txt file with one package.
Intermediate Exercises
- Install Flask and pytest in a virtual environment.
- Generate a requirements file using pip freeze.
- Create a second virtual environment and install the recorded requirements.
- Modify a version constraint and observe pip's resolution behavior.
- Remove an unused package and update the dependency documentation.
Advanced Exercises
- Create separate runtime and development requirements files.
- Investigate a dependency conflict in a disposable test environment.
- Prepare setup instructions for a team Python project.
- Compare requirements.txt with dependency metadata in pyproject.toml.
- Configure a continuous integration workflow to install dependencies and run automated tests.
22. Interview Questions and Answers
Q1. What is pip in Python?
Answer: pip is a package installer used to install and manage Python packages and their dependencies.
Q2. What is requirements.txt?
Answer: It is a text file commonly used to document package requirements so developers can install the dependencies needed by a project.
Q3. How do you install dependencies from requirements.txt?
Answer: Run python -m pip install -r requirements.txt from the project directory or specify the path to the file.
Q4. What does pip freeze do?
Answer: It displays installed distributions and their versions in a requirements-style format. The output can be redirected to a file to record an environment snapshot.
Q5. Why should you use a virtual environment?
Answer: A virtual environment isolates project dependencies and helps prevent package version conflicts between projects.
Q6. What is the difference between pip install and pip install -r?
Answer: The first installs packages specified on the command line. The second reads package requirements from a file.
Q7. Why are package version constraints important?
Answer: They control which releases can be installed and help maintain compatibility and predictable application behavior.
Q8. Does pip freeze guarantee an identical environment everywhere?
Answer: No. It records installed distributions and versions, but operating systems, Python versions, platform-specific dependencies, and other conditions can affect the resulting environment.
Q9. How can dependency conflicts be identified?
Answer: Review pip's installation messages and run python -m pip check to identify incompatible requirements among installed packages.
Q10. Should a virtual environment be committed to Git?
Answer: Usually not. Commit the application source code and dependency declarations, and exclude the virtual environment directory from version control.
Q11. What is a transitive dependency?
Answer: A transitive dependency is a package required by another package that the application uses.
Q12. What should be done before upgrading production dependencies?
Answer: Review compatibility and release information, test the changes in a controlled environment, run automated tests, and validate the application before deployment.
23. Quick Revision Notes
| Concept |
Key Point |
| pip |
Installs and manages Python packages. |
| requirements.txt |
Documents project package requirements. |
| Virtual environment |
Isolates project dependencies. |
| pip install |
Installs specified packages. |
| pip freeze |
Displays installed packages and versions. |
| pip install -r |
Installs dependencies listed in a file. |
| pip check |
Checks installed packages for incompatible requirements. |
| Version constraints |
Specify which package versions are permitted. |
| Dependency testing |
Helps identify regressions after package changes. |
24. Conclusion
Managing dependencies using pip and requirements.txt is a fundamental Python development skill. pip provides commands for installing, upgrading, inspecting, and removing packages, while requirements.txt documents the packages needed to configure a project.
Combining a dedicated virtual environment, sensible version constraints, updated dependency documentation, and automated testing makes Python applications easier to maintain and deploy. For larger or production applications, a tested dependency-locking workflow can provide additional control over the software environment.
Continue learning with the JustAcademy Python Training Course. Visit the JustAcademy official website or register for a course demo to explore learning opportunities.